package com.dy.nygl.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.mapper.CostAnalysisMapper;
import com.dy.nygl.service.MachineService;
import com.dy.nygl.service.ReportSummaryService;

/**
 * 本类主要完成：
 * toSumUp.action ---> addReportTimeAndTask()
 * selectTime.action ---> selectByTimeAndTypeTask()
 * efficiency.action ---> getEveryMonthTotal()、getEveryYearTotal()、getTaskNameTotal(),详细点击进入
 * energyPlanManagement.action ---> efficiencyManager() 
 * 
 * @author xiao
 * @version 1.0
 */

@Controller
@RequestMapping("/reportSummary")
public class ReportSummaryController {
	
	@Resource(name="reportSummaryService")
	private ReportSummaryService reportSummaryService;
	
	@Resource(name="costAnalysisDao")
	private CostAnalysisMapper costAnalysisDao;
	
	@Resource(name="machineService")
	private MachineService machineService;
	
	// 手动插入数据进行 年、月、 任务名 总结  ----> 将数据表中信息 手动添加到report 表中完成总结，也可通过com.dy.nygl.webloading包中的ReportLoading启动线程调用。 参数返回时注意判断
	@RequestMapping("/toSumUp.action")
	public ModelAndView toSumUp(@RequestParam("taskName")String taskName) throws Exception{
		System.err.println("进入 年、月、 任务名 总结.........");
		reportSummaryService.addReportTimeAndTask(taskName);
		return null;
	}
	
	// 查询报告表中的数据  年、月、任务名 查询  ----> 负责查询report表，通过时间能源、任务名以及年、月 总结。  参数返回时注意判断
	@RequestMapping("/selectTime.action")
	public ModelAndView selectTime(@RequestParam("object")String object,@RequestParam("time")String time){
		System.err.println("进入 年、月、任务名 查询.........");
		reportSummaryService.selectByTimeAndTypeTask(object, time);
		return null;
	}
	
	@RequestMapping("/selectYear.action")
	@ResponseBody
	public Map<String,Object> selectYear(@RequestParam("object")String object) throws ParseException{
		System.err.println("进入 年 查询.........");
		String time = "";
		Map<String , Object> map = new LinkedHashMap<>();
		List<String> listType = costAnalysisDao.selectEnergyType();
		List<String> Total = reportSummaryService.selectByTimeAndTypeTask(object,time);
		List<Double> arr = new ArrayList<>();
		for(int i = 0 ; i<Total.size();i++){
			arr.add(Double.valueOf(Total.get(i)));
		}
		map.put("total", arr);
		map.put("listType", listType);
		map.put("object", object);
		System.out.println(map);
		return map;
	}
	
	@RequestMapping("/selectReport.action")
	@ResponseBody
	public ModelAndView selectReport(@RequestParam("time")String time) throws ParseException{
		String object = "";
		List<String> listType = costAnalysisDao.selectEnergyType();
		List<String> expectedValueAve = reportSummaryService.getEveryYearTotal(time);
		List<Double> Total = machineService.selectAllDataByEnergySer(time);
		List<String> actual = reportSummaryService.selectByTimeAndTypeTask(time,object);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		for(int i = 0;i < listType.size();i++){
			Map<String,Object> map = new LinkedHashMap<>();
			map.put("listType",listType.get(i));
			map.put("Total",Total.get(i));
			map.put("expectedValueAve",expectedValueAve.get(i));
			map.put("actual",actual.get(i));
			list.add(map);
		}
		
		ModelAndView model = new ModelAndView();
		model.addObject("list",list);
		model.setViewName("costManager_select");
		return model;
		
	}

	@RequestMapping("/selectAllDataBySetTime.action")
	@ResponseBody
	public Map<String,Object> selectAllDataBySetTime(@RequestParam("startTime")String starttime) throws ParseException{
		System.err.println("selectAllDataByTime....");
		Map<String,Object> map = new LinkedHashMap<>();
		List<Double> monthTotal = reportSummaryService.selectTotal(starttime);
		List<Double> price = costAnalysisDao.selectPrice();
		List<Double> totalCost = new ArrayList<>();
		for( int i = 0; i<price.size(); i++ ){
			totalCost.add(monthTotal.get(i)*price.get(i));
		}
		List<String> listType = costAnalysisDao.selectEnergyType();
		map.put("monthTotal", monthTotal);
		map.put("listType", listType);
		map.put("totalCost", totalCost);
		map.put("starttime",starttime);
		System.out.println(map);
		return map;
	}
	
	@RequestMapping("/selectAllDataByTime.action")
	@ResponseBody
	public Map<String,Object> selectAllDataByTime(@RequestParam("startTime")String starttime) throws ParseException{
		System.err.println("selectAllDataByTime....");
		Map<String,Object> map = new LinkedHashMap<>();
		List<Double> monthTotal = reportSummaryService.selectTotal(starttime);
		List<Double> price = costAnalysisDao.selectPrice();
		List<Double> totalCost = new ArrayList<>();
		for( int i = 0; i<price.size(); i++ ){
			totalCost.add(monthTotal.get(i)*price.get(i));
		}
		List<String> listType = costAnalysisDao.selectEnergyType();
		map.put("monthTotal", monthTotal);
		map.put("listType", listType);
		map.put("totalCost", totalCost);
		map.put("starttime",starttime);
		System.out.println(map);
		return map;
	}

	// 能源效率分析  ---- >  将总的信息进行分析，分析每年基本的各项能源消耗量和总成本、分析任务名下的
	// 搜索月份返回72个数，以月份为第一排序，能源类型为第二排序的能源
	@RequestMapping("/efficiency.action")
	public void efficiency(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		System.err.println("进入efficiency。。。。");
		request.setCharacterEncoding("utf-8");
		reportSummaryService.efficiencyHub(request.getParameter("time"));
	}

	// 能源效率计划管理  ---- > 通过对 相同后缀名的任务名
	@RequestMapping("/energyPlanManagement.action")
	public void energyPlanManagement(HttpServletRequest request,HttpServletResponse response){
		reportSummaryService.efficiencyManager(request.getParameter("time"), request.getParameter("totalProfit"));
	}
	
	
}
